package com.vlong.town.controller.admin.login;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.organization.Organization;
import com.vlong.town.entity.role.Role;
import com.vlong.town.entity.user.User;
import com.vlong.town.service.organization.OrganizationService;
import com.vlong.town.service.role.RoleService;
import com.vlong.town.service.user.UserService;
import com.vlong.town.util.common.TownConstant;

import java.util.List;

/**
 * 系统登录Controller
 * @author Assassin
 */
@Controller
public class LoginController extends BaseController {

    // Fields

    @Autowired
    private UserService userService;// 用户业务实现类
    @Autowired
    private OrganizationService organizationService;//机构业务实现类
    @Autowired
    private RoleService roleService;//权限业务实现类

    /**
     * 进入网站首页
     * @return login/login
     */
    @GetMapping("/")
    public String gotoIndex() {
        return "redirect:/client/index/main";
    }

    /**
     * 进入登录页面
     * @return login/login
     */
    @GetMapping("/admin/login")
    public String gotoLoginPage() {
        return "admin/login/login";
    }

    @PostMapping("/executeLogin")
    @ResponseBody
    public DataMessage executeLogin(
            @RequestParam(name="account") String account,
            @RequestParam(name="password") String password) {
        DataMessage dm = new DataMessage();
        if(!this.validateStringProperty(account, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("帐号参数错误");
        }
        if(!this.validateStringProperty(password, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("密码参数错误");
        }
        // 登录验证
        User loginUser = this.userService.queryLogin(1, account, password);
        if(loginUser == null) {
            dm.setResult(TownConstant.RESULT_CODE_DATA_ERROR);
            dm.setMessage("登录账号或密码不正确");
        } else {
        	// 查询用户机构信息
    		List<Organization> orgList = this.organizationService.queryUserOrgs(loginUser.getUserId());
    		Organization organization = orgList.get(0);
        	super.setOrgId(organization.getOrgId());//机构id
        	super.setUserId(loginUser.getUserId());//登陆用户id
        	super.setRealName(loginUser.getRealName());//登陆用户名字
        	super.setTelphone(loginUser.getTelphone());//账号（手机号）
        	//查询当前登录人的角色--判断是否显示选择角色页面--只有一个角色不显示选择角色页面
			List<Role> userRoles = this.roleService.queryRoleByUserId(getUserId());
            // Session
            request.getSession().setAttribute(TownConstant.TOWN_SESSION_USER, loginUser);
            request.getSession().setAttribute(TownConstant.TOWN_SESSION_USER_TYPE, TownConstant.TOWN_SESSION_USER_TYPE_ADMIN);
            System.out.println(request.getSession().getAttribute(TownConstant.TOWN_SESSION_USER_TYPE));
            dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setData(userRoles.size());
            dm.setMessage("登录成功");
        }
        return dm;
    }
    
    /**
     * 跳转选择角色页面
     * @return
     */
    @GetMapping("/role")
    public String role(){
		return "admin/index/selectRole";
	}
    
    

}
